============================ Cinepak for Sega - Breakdown by Tasco Deluxe < tasco.deluxe @ gmail.com > First edition: 2006.04.27 ============================ Copyright (c) 2006, Tasco Deluxe. Some rights reserved. This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. DO NOT modify or redistribute this document without reading the license. Introduction ============ Cinepak for Sega is a video codec designed for encoding/decoding video used in Sega CD titles. Cinepak codecs use vector quantization to compress frames of video. Cinepak for Sega was designed to be easily decompressed by an underpowered CPU (Motorola M68000 @ 12.5MHz) with the maximum throughtput of a single-speed CD-ROM drive (150 KB/s). FILM file format (briefly) -------------------------- The FILM file format is a multimedia container format developed by Sega that is used to deliver audio/video for display on Sega consoles including the Sega CD and Sega Saturn. For a complete breakdown of the FILM format, read Mike Melanson's article "Description of the Sega FILM/CPK File Format". The latest version of the article (v1.4, as of April 27, 2006) can be found at: http://www.multimedia.cx/film-format.txt A Wiki version of the same article can be found at: http://wiki.multimedia.cx/index.php?title=Sega_FILM The FOURCC codes used to indicate Cinepak for Sega encoded files include 'sega', 'SEGA', and 'SEG4'. There are no discernable differences in the encoding of their respective video frames. Any audio samples paired with the video frames are stored in a format completely independent of Cinepak for Sega. This is most often Sega CD's native PCM format (8-bit sign-magnitude). Cinepak for Sega codec (overview) ================================= A frame of video is divided into 8x8 pixel blocks. These blocks correspond to the 8x8 cells recognized by the Sega Genesis VDP. The cell format is 4 bits per pixels and, thus, 32 bytes per cell. Each block is encoded into 8-bit vectors that either reference entries into one of two codebooks (lookup tables) or that represent raw block data. The method used for each block is determined by a list of 2-bit values (method flags). These values are: 00 = no vectors - block data is unchanged [inter-frame only] 01 = 32 vectors as raw block data - no translation necessary 10 = 8 vectors referencing 32-bit codebook entries 11 = 16 vectors referencing 16-bit codebook entries Two codebooks are loaded per frame, a 32-bit codebook which contains a maximum of 256 32-bit words (1 kilobyte). and a 16-bit codebook which contains a maximum of 256 16-bit words (512 bytes), Unlike the Cinepak CVID format, a frame is not divided into strips and the decoded block data (color values) require no conversion. 'SM' Video Frame ================ Frames of Cinepak for Sega encoded video are stored in chunks that begin with the text 'SM'. A 'SM' frame consists of the following: - Header (12 bytes) - Palette Data (32 bytes per palette) - Palette Map (2 bits per 8x8 block) [if more than one palette is loaded] - 32-bit Codebook - 16-bit Codebook - Method Flags (2 bits per 8x8 block) - Vector List Header ------ +0000.W = 'SM' +0002.W = bits -> ???? ???? ??X? ??PP ? = unknown/unused (usually clear) X = unknown (usually set) PP = palette count 00 1 palette 01 2 palettes 10 3 palettes 11 4 palettes +0004.L total byte count (decoded)* [rounded up by 32 bits] +0008.W frame width in 8x8 blocks +000A.W frame height in 8x8 blocks * Total byte count includes: Header size Palette Data size Palette Map size Decoded Frame Data size (width in blocks * height in blocks * 32) Palette Data ------------ Palettes are stored in the Sega Genesis VDP's native format. The native format, at 9 bits per pixel, is: 0000 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR 0BGR B = bits -> bbb0 (blue) G = bits -> ggg0 (green) R = bits -> rrr0 (red) The palette map is a list of two-bit values for each block. A value indicates the index of the palette used to display the block/cell. The values are read from the highest bit to the lowest. The map data is always rounded up by 32 bits. If only 1 palette is included in the frame, map data is not included. If 2 palettes are included, values are still two bits*. * Can't confirm this without seeing a frame that uses 2 palettes. Codebooks --------- The first 32-bit word after the palette data/map is the size of the 32-bit codebook in bytes. The codebook data follows. The maximum size for the table is 1 kilobyte (256 * 4 bytes). The first 32-bit word after the 32-bit codebook data is the size of the 16-bit codebook in bytes. As before, the codebook data follows. The maximum size is 512 bytes (256 * 2 bytes). Method Flags ------------ Method flags are listed as 2-bit values for each block -- similar to the palette map. A value indicates the method used to encode the corresponding block. The method flags data is always rounded up by 32 bits. Vector List ----------- The vector list is a series of 8-bit values whose individual meanings is determined by the methods used to encode blocks. Decoding the frame ================== For each block, a 2-bit value is read from the method flags. Thus, each byte of the method flags (8 bits) indicates the methods of 4 consecutive blocks. The values are read from the highest bit to the lowest. Vectors are read linearly from the vector list when needed. Method 00 - no update [inter-frame only] --------------------- The block is not updated, leaving the data from the previous frame. This method doesn't require any vectors. This method only makes sense when used as part of an inter-frame (basically, a modified version of the previous frame). Inter-frame encoding might not be used at all in Sega CD titles because the normal method of decoding uses buffer swapping (for smooth frame rates) which doesn't allow for such updates. Also the constant updating of palettes doesn't make it likely to work. Method 01 - 32 vectors / no translation --------------------------------------- The block data is copied straight from the vector list (32 bytes). This method is used for unique block data that can't be reliably reused within the frame or that won't fit in either codebook. Method 10 - 8 vectors / 32-bit codebook --------------------------------------- The block data is copied from the 32-bit codebook using 8 vectors from the vector list to index. The vectored data is referenced in an interleaved fashion. For vectors a, b, c, d, e, f, g, and h: A = codebook32[a].L , B = codebook32[b].L C = codebook32[c].L , D = codebook32[d].L E = codebook32[e].L , F = codebook32[f].L G = codebook32[g].L , H = codebook32[h].L block[+0 ] <= A high word , B high word , A low word , B low word block[+8 ] <= C high word , D high word , C low word , D low word block[+16] <= E high word , F high word , E low word , F low word block[+24] <= G high word , H high word , G low word , H low word Method 11 - 16 vectors / 16-bit codebook ---------------------------------------- The block data is copied from the 16-bit codebook using 16 vectors from the vector list to index. As with the previous method, the vectored data is referenced in an interleaved fashion. For vectors a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, and p: A = codebook16[a].W , B = codebook16[b].W C = codebook16[c].W , D = codebook16[d].W E = codebook16[e].W , F = codebook16[f].W G = codebook16[g].W , H = codebook16[h].W I = codebook16[i].W , J = codebook16[j].W K = codebook16[k].W , L = codebook16[l].W M = codebook16[m].W , N = codebook16[n].W O = codebook16[o].W , P = codebook16[p].W block[+0 ] <= A high byte , B high byte , C high byte , D high byte block[+4 ] <= A low byte , B low byte , C low byte , D low byte block[+8 ] <= E high byte , F high byte , G high byte , H high byte block[+12] <= E low byte , F low byte , G low byte , H low byte block[+16] <= I high byte , J high byte , K high byte , L high byte block[+20] <= I low byte , J low byte , K low byte , L low byte block[+24] <= M high byte , N high byte , O high byte , P high byte block[+28] <= M low byte , N low byte , O low byte , P low byte Afterthoughts ============= For information on the Cinepak CVID format, read Dr. Tim Ferguson's article "Cinepak (CVID) stream format for AVI and QT". The article can be found at: http://www.csse.monash.edu.au/~timf/videocodec/cinepak.txt To get the latest version as well as the latest information of MERLiX's SegaCD Movie Viewer, check out the project page at: http://www.eidolons-inn.net/tiki-index.php?page=SegaCD+Movie+Viewer ###